﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using FF.Method;
using FF.Entity;
using System.IO;

namespace FF
{
    public partial class FormGoodManager : Form
    {
        public FormGoodManager()
        {
            InitializeComponent();
        }
        private string userId;
        public FormGoodManager(string userid)
        {
            InitializeComponent();
            this.userId = userid;
        }

        private void FormQueryGood_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxKeyword.Text == "")
            {
                MessageBox.Show("请输入查找条件！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxKeyword.Focus();
                return;
            }
            DataSet ds = GoodMethod.getGoodByKeyword(textBoxKeyword.Text.Trim());
            if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
            {
                MessageBox.Show("没有相关记录！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxKeyword.Focus();
                dataGridView1.DataSource = ds.Tables[0];
                //dataGridView1.DataSource = null;
                return;
            }
            dataGridView1.DataSource = ds.Tables[0];
            return;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataSet ds = GoodMethod.getGoodByKeyword("");
            if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
            {
                MessageBox.Show("没有相关记录！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxKeyword.Focus();
                dataGridView1.DataSource = ds.Tables[0];
                return;
            }
            dataGridView1.DataSource = ds.Tables[0];
            textBoxKeyword.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string selectedGoodId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            Good good = GoodMethod.getGoodById(selectedGoodId);

            if (good != null)
            {
                FormGood form = new FormGood(selectedGoodId, FormGood.ACTION_TYPE_MODIFY_DELETE);
                //form.user = selectedUser;
                form.Owner = this;
                form.ShowDialog();
                if (textBoxKeyword.Text.Trim() == "")
                {
                    button2_Click(sender, e);
                }
                else
                {
                    button1_Click(sender, e);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = false;
            saveFileDialog.Title = "Export Excel File To";
            DateTime now = DateTime.Now;
            saveFileDialog.FileName = "商品信息" + now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + "-" + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0');
            //saveFileDialog.ShowDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string str = "";
            try
            {
                //写标题
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += dataGridView1.Columns[i].HeaderText;
                }
                sw.WriteLine(str);
                //写内容
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    string tempStr = "";
                    for (int k = 0; k < dataGridView1.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            tempStr += "\t";
                        }
                        if (dataGridView1.Rows[j].Cells[k].Value != null)
                            tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
                    }

                    sw.WriteLine(tempStr);
                }
                sw.Close();
                myStream.Close();
                MessageBox.Show("导出成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }          
        }
    }
}
